package com.intelligentcar.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.intelligentcar.springboot.bean.Manager;
import com.intelligentcar.springboot.service.ManagerServer;
import com.intelligentcar.springboot.service.serverImpl.Md5;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin
@RestController
public class ManagerController {
    @Autowired
    private ManagerServer managerServerImpl;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private Md5 md5;

    @RequestMapping("/queryrepair")
    @ResponseBody
    //查询所有的维修人员
    public Map queryrepair() {
        Map map = new HashMap();
        map.put("list", managerServerImpl.queryrepair());
        return map;
    }

    @RequestMapping("/manageradd")
    @ResponseBody
    //新增后台人员
    public Map manageradd(@RequestBody Manager manager) {
        Map map = new HashMap();
        manager.setUpwd(md5.MD5(manager.getUpwd()));
        manager.setAddTime(new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date()));
        map.put("code", managerServerImpl.manageradd(manager));
        return map;
    }


    @RequestMapping("/selectmanager")
    @ResponseBody
    //查询后台人员表中的所有人员
    public Map selectmanager(@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("total", managerServerImpl.count());
        map.put("list", managerServerImpl.selectmanager(new RowBounds((manager.getPage() - 1) * manager.getSize(), manager.getSize())));
        return map;
    }




    @RequestMapping("/update")
    @ResponseBody
    //修改后台人员信息
    public Map update(@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("code", managerServerImpl.update(manager));
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    //删除后台人员信息
    public Map delete(@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("code", managerServerImpl.delete(manager));
        return map;
    }

    @RequestMapping("/querybackstage")
    @ResponseBody
    //查询后台人员信息
    public Map querybackstage (@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("total", managerServerImpl.count1(manager));
        map.put("list", managerServerImpl.querybackstage(manager,new RowBounds((manager.getPage() - 1) * manager.getSize(), manager.getSize())));
        return map;
    }


    @RequestMapping("/querybackstage1")
    @ResponseBody
    //查询后台人员信息
    public Map querybackstage1 (@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("total", managerServerImpl.count2(manager));
        map.put("list", managerServerImpl.querybackstage1(manager,new RowBounds((manager.getPage() - 1) * manager.getSize(), manager.getSize())));
        return map;
    }

    @RequestMapping("/querybackstage2")
    @ResponseBody
    //查询后台人员信息
    public Map querybackstage2 (@RequestBody Manager manager) {
        Map map = new HashMap();
        map.put("total", managerServerImpl.count3(manager));
        map.put("list", managerServerImpl.querybackstage2(manager,new RowBounds((manager.getPage() - 1) * manager.getSize(), manager.getSize())));
        return map;
    }


    @RequestMapping("/updateSelf")
    @ResponseBody
    public boolean updateSelf(HttpServletRequest req, @RequestBody Manager manager){
        return managerServerImpl.updateSelf(req,manager);
    }

    @RequestMapping("/updateSelfPwd888")
    @ResponseBody
    public boolean updateSelfPwd(@RequestBody Manager manager){
        return managerServerImpl.updatePwd(manager);
    }

    @RequestMapping("/Login")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map<String,Object> getLogin(HttpServletRequest req, @RequestBody  Manager manager){
        Map<String,Object>  map = managerServerImpl.login(req, manager);
//        System.out.println("状态:"+ManagerServerImpl.login(manager).getUstate());
        return map;
    }

    @RequestMapping("/selectInfor")
    @ResponseBody
    //查询后台人员信息
    public Manager selectInfor (@RequestBody Manager manager) {
        return managerServerImpl.selectInfor(manager);
    }

    @RequestMapping("/updateHead")
    @ResponseBody
    //查询后台人员信息
    public int updateHead (@RequestBody Manager manager) {
        return managerServerImpl.updateHead(manager);
    }

    //可上传文件类型定义
    private List fileTypes = new ArrayList() {
        {
            add("jpg");
            add("png");
        }
    };

    @Value("${server.port}")
    private String servePrort;

    @RequestMapping("uploadHead")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file) throws FileNotFoundException {
        String IP = "1";
        try {
            /*获取IP*/
            IP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String pathStr = ResolverUtil.Test.class.getResource("/").getPath();
        String pathSbu = pathStr.substring(1);/*前面有个"/"要去掉*/

        JSONObject jsonRsult = new JSONObject();
        if (file.isEmpty()) {
            jsonRsult.put("code", 201);
            jsonRsult.put("msg", "请选择文件");
            return jsonRsult.toJSONString();
        }
        String originalFilename = file.getOriginalFilename();
        String AdvertPicutre = "http://" + IP + ":" + servePrort + "/vending/" + originalFilename;
        String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        if (!fileTypes.contains(fileType)) {
            jsonRsult.put("code", 201);
            jsonRsult.put("msg", "仅支持 .jpg .png 文件上传");
            return jsonRsult.toJSONString();
        }
        String path = pathSbu + "/static/vending/";
        Path filePath = Paths.get(path + originalFilename);
        try {
            byte[] fileBytes = file.getBytes();
            Files.write(filePath, fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

        jsonRsult.put("code", 1);
        jsonRsult.put("msg", AdvertPicutre);
        return jsonRsult.toJSONString();
    }

    @RequestMapping("/pwdCheck")
    @ResponseBody
    //查询所有的维修人员
    public boolean pwdCheck(@RequestBody Manager manager) {
        return managerServerImpl.pwdCheck(manager);
    }

}
